{% extends 'base.html' %}
{% block link %}
    <link rel="stylesheet" href="/static/layui/css/login.css" media="all">
{% endblock %}

{% block content %}
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
        <legend id="titleLeg"> 修改密码</legend>
    </fieldset>

    <div class="layui-row">

        <div class="layui-col-xs6 layui-col-lg-offset1">
            <div class="layadmin-user-login-box layadmin-user-login-body layui-password layui-form">
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                           for="LAY-user-login-oldpassword"></label>
                    <input type="password" name="oldpassword" id="LAY-user-login-oldpassword" lay-verify="required|pass"
                           placeholder="当前密码"
                           class="layui-input">
                </div>
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                           for="LAY-user-login-newpassword"></label>
                    <input type="password" name="newpassword" id="LAY-user-login-newpassword" lay-verify="required|pass"
                           placeholder="新密码"
                           class="layui-input">
                </div>
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                           for="LAY-user-login-renewpw"></label>
                    <input type="password" name="renewpw" id="LAY-user-login-renewpw" lay-verify="required|pass"
                           placeholder="确认新密码"
                           class="layui-input">
                </div>

                <div class="layui-form-item" style="text-align: center">
                    <button class="layui-btn" lay-submit lay-filter="LAY-user-changepass-submit">确认修改</button>
                </div>

            </div>

        </div>
    </div>


{% endblock %}

{% block script %}
    {{ super() }}
    <script>
        layui.use('form', function () {
            var form = layui.form;
            form.render();

            form.on('submit(LAY-user-changepass-submit)', function (obj) {
                var field = obj.field;
                if (field.newpassword !== field.renewpw)
                    return layer.msg('两次密码输入不一致');

                form.verify({
                    //我们既支持上述函数式的方式，也支持下述数组的形式
                    //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
                    pass: [
                        /^[\S]{6,12}$/
                        , '密码必须6到12位，且不能出现空格'
                    ]
                });
                $.ajax({
                    url: '{{ url_for("auther.RegisterApi") }}',
                    type: "put",
                    dataType: "json",
                    data: field,
                    success: function (data) {
                        console.log(data)
                        if (data.code == 10000) {
                            layer.msg('密码修改成功, 请重新登陆', {icon: 1})
                            setInterval(function () {
                                window.open('{{ url_for("auther.logout") }}')
                            },2000)
                        } else if (data.code == 10001) {
                            layer.msg('请先登陆', {icon: 5})
                        } else if (data.code == 10002) {
                            layer.msg('旧密码输入错误', {icon: 5})
                        }
                    }
                })
            })
        })
    </script>
{% endblock %}
